package com.zwj.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zwj.bo.BatchBo;
import com.zwj.bo.InsertBatchCourseBo;
import com.zwj.dto.PaginationDate;
import com.zwj.dto.Result;
import com.zwj.dto.ReturnDate;
import com.zwj.entity.BatchCourseEntity;
import com.zwj.entity.BatchEntity;
import com.zwj.entity.CourseInfoEntity;
import com.zwj.enums.IsDeletedEnum;
import com.zwj.enums.StatusEnum;
import com.zwj.mapper.BatchMapper;
import com.zwj.service.BatchService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwj.utils.DataCheckUtils;
import com.zwj.utils.DateUtils;
import com.zwj.utils.PaginationUtils;
import com.zwj.utils.TokenUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zwj
 * @since 2021-11-04
 */
@Service
public class BatchServiceImpl extends ServiceImpl<BatchMapper, BatchEntity> implements BatchService {

    private final BatchMapper batchMapper;
    public BatchServiceImpl(BatchMapper batchMapper){
        this.batchMapper=batchMapper;
    }

    @Override
    public Result selectBatch(BatchBo batchBo) {
        //数据校验
        DataCheckUtils.isValid(PaginationUtils.isPagingParameters(batchBo.getNumber(), batchBo.getPage()), "分页参数错误!");
        int page = batchBo.getPage();
        batchBo.setPage(PaginationUtils.getPageIndex(batchBo.getPage(), batchBo.getNumber()));
        //查询批次
        List<Object> result = batchMapper.selectBatch(batchBo);
        int sum = ((List<Integer>) result.get(1)).get(0);
        //记录总数为0
        if (sum == 0) return Result.error().message("查询结果为空");
        PaginationDate paginationDate = PaginationUtils.getPaginationDate(batchBo.getNumber(), page, sum);
        //当前页大于总页数
        if (PaginationUtils.isValid(paginationDate))
            return Result.error().message("分页参数错误!");
        List<BatchEntity> batchEntities =  (List<BatchEntity>) result.get(0);
        return Result.ok().data(new ReturnDate(paginationDate,batchEntities));

    }

    @Override
    public Result updateBatchStatus(BatchEntity batchEntity) {
        //BatchEntity batchEntity = new BatchEntity();
        batchEntity.setStatus(batchEntity.getStatus());
        UpdateWrapper<BatchEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(BatchEntity::getId, batchEntity.getId());
        int row = batchMapper.update(batchEntity, updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("编辑批次状态失败");
        }
        return Result.ok();
    }

    @Override
    public Result addBatch(BatchBo batchBo, String userId) {
        batchBo.setIsDeleted(IsDeletedEnum.NO.getKey());
        batchBo.setStatus(StatusEnum.INVALID.getKey());

        String userName = batchMapper.selectNameById(userId);
        batchBo.setCreateBy(userName);

        batchBo.setCreateTime(DateUtils.getCurrentTime());
        int row = batchMapper.addBatch(batchBo);
        //判断插入结果
        if (row == 0) {
            return Result.error().message("添加批次失败");
        }
        return Result.ok();
    }

    @Override
    public Result updateBatch(BatchEntity batchEntity, String userId) {
        String userName = batchMapper.selectNameById(userId);
        System.out.println(userName);
        batchEntity.setUpdateBy(userName);
        batchEntity.setUpdateTime(DateUtils.getCurrentTime());
        UpdateWrapper<BatchEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(BatchEntity::getId, batchEntity.getId());
        int row = batchMapper.update(batchEntity, updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("编辑批次信息失败");
        }
        return Result.ok();
    }

    @Override
    public Result deleteBatch(BatchBo batchBo) {
        DataCheckUtils.isNotBlank(batchBo.getId(), "批次id不能为空!");
        BatchEntity batchEntity = new BatchEntity();
        batchEntity.setIsDeleted(IsDeletedEnum.YES.getKey());
        UpdateWrapper<BatchEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(BatchEntity::getId, batchBo.getId()).eq(BatchEntity::getIsDeleted,IsDeletedEnum.NO.getKey());
        int row = batchMapper.update(batchEntity, updateWrapper);
        //判断删除结果
        if (row == 0) {
            return Result.error().message("删除课程失败");
        }
        return Result.ok();
    }

    @Override
    public Result insert(InsertBatchCourseBo insertBatchCourseBo) {
        Integer[] ids = insertBatchCourseBo.getIds();
        int batchId = insertBatchCourseBo.getId();
        for (Integer courseId : ids) {
            BatchCourseEntity infoEntity = new BatchCourseEntity();
            infoEntity.setBatchId(String.valueOf(batchId));
            infoEntity.setCourseId(String.valueOf(courseId));
            BatchCourseEntity batchCourseEntity = batchMapper.selectBatchCourseBy(infoEntity);
            if (batchCourseEntity==null){
                int rel = batchMapper.insertinfo(infoEntity);
                if (rel<1){
                    return Result.error().message("添加失败");
                }
            }
        }
        return Result.ok();
    }

}
